package leetcode;

import java.util.Arrays;

/**
 * 描述:148. 排序链表
 *
 * @author Madison You
 * @created 21:32
 */
public class Mid_148_SortList {

    public static void main(String[] args) {
        Mid_2_AddTwoNumbers mid_2_addTwoNumbers = new Mid_2_AddTwoNumbers();
        int[] arr = {1,2,3,45,5};
        ListNode node = mid_2_addTwoNumbers.createNode(arr);
        ListNode listNode = sortList(node);
        System.out.println(listNode);
    }

    public static ListNode sortList(ListNode head) {
        int i = 0;
        ListNode a = head;
        while(a!=null ) {
            i++;
            a = a.next;
        }
        int[] ints = new int[i];
        int u = 0;
        while (head != null) {
            ints[u++] = head.val;
            head = head.next;
        }

        Arrays.sort(ints);
        ListNode dynamic = new ListNode();
        ListNode other = dynamic;
        for(int j = 0 ; j < ints.length ; j ++) {
            other.next = new ListNode(ints[j]);
            other = other.next;
        }
        return dynamic.next;
    }





}
